package com.glory.dgcpcpda.fragment;

import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.activity.SettingActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pda.platform.ui.ui_pdaplatform.adapter.FreeUI_TabIndexGridAdapter;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseFragment;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabIndexGridEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.glory.dgcpcpda.utils.MyUtils.setPicToDrawable;

/**
 * FileName: SearchFragment
 * Author: XJH
 * Date: 2020/5/13 14:41
 * Description: 查询界面
 * History:
 */
public class SearchFragment extends FreeUI_BaseFragment {

    @BindView(R.id.tvTtitle)
    TextView tvTitle;
    @BindView(R.id.rvList)
    RecyclerView rvList;

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_search;
    }

    @Override
    protected void initView() {
        tvTitle.setText("查询");
        rvList.setLayoutManager(new GridLayoutManager(getActivity(), 3));
    }

    @Override
    protected void initData() {
        try {
            initArea();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void initArea() throws IOException{
        InputStream searchStream = getActivity().getApplicationContext().getResources().getAssets().open("search.txt");
        String searchJson = FreeApi_StringUtils.getString(searchStream);
        List<FreeUI_TabIndexGridEntity> entity = new Gson().fromJson(searchJson, new TypeToken<List<FreeUI_TabIndexGridEntity>>() {
        }.getType());
        FreeUI_TabIndexGridAdapter tabIndexGridAdapter = new FreeUI_TabIndexGridAdapter(setPicToDrawable(getActivity(),entity), R.layout.free_ui_glory_adapter_tab_index_grid);
        tabIndexGridAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        tabIndexGridAdapter.isFirstOnly(true);
        rvList.setAdapter(tabIndexGridAdapter);
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return null;
    }

    @OnClick(R.id.ivSetting)
    public void onViewClicked() {
        FreeApi_Utils.skipWithExtraIntent("header", getActivity(), SettingActivity.class);
    }
}
